package com.example.user.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.common.component.UserCtrlCom;
import com.example.common.control.ICallback;
import com.example.common.model.GameResp;
import com.example.common.model.UserApiGetUserFavorResp;
import com.example.common.user.GlobalUser;
import com.example.user.model.UserGame;

import java.util.ArrayList;
import java.util.List;

public class MyGamePageViewModel extends ViewModel {
    // TODO: Implement the ViewModel
    private List<GameResp> userGames;
    private UserCtrlCom userCtrlCom;
    private MutableLiveData<String> remainMsg=new MutableLiveData<>();//提示消息
    private MutableLiveData<Boolean> isLoadDone=new MutableLiveData<>();//加载是否完成

    public MyGamePageViewModel(){
        userCtrlCom=new UserCtrlCom();
        initData();
    }

    private void initData(){
        userGames=new ArrayList<>();
    }

    /**
     * 加载用户收藏游戏列表
     */
    public void loadGamesList(){
        isLoadDone.setValue(false);
        userCtrlCom.getUserIdFavor(GlobalUser.TOKEN, GlobalUser.ID, new ICallback() {
            @Override
            public void onSuccess(Object response) {
                UserApiGetUserFavorResp resp=(UserApiGetUserFavorResp)response;
                if(resp.getList()!=null){
                    userGames=resp.getList();

                    isLoadDone.setValue(true);//fragment中监听后刷新adapter
                }
            }

            @Override
            public void onFailed(String mes) {
                remainMsg.setValue(mes);
            }
        });
    }

    public List<GameResp> getUserGames() {
        return userGames;
    }

    public MutableLiveData<Boolean> getIsLoadDone() {
        return isLoadDone;
    }

    public MutableLiveData<String> getRemainMsg() {
        return remainMsg;
    }
}